package com.oceansoft.mobile.biz.api.controller;

/**
 * Created by liujie on 2017/7/20.
 */

import org.apache.commons.io.IOUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 *<p>ftp连接管理(使用apache commons-net-1.4.1 lib)</p>
 *
 *
 */
public class FtpConManager {
    private static FtpConManager instance;
    private FTPClient ftpClient =null;

    private static String ftphost="172.16.1.92";
   // private static String ftphost="172.16.30.38";
    private static String ftpuser="ftpup";
    private static String ftppassword="ftp";
    private static String ftppath="/face/";

    /**
     * get FtpConManager
     * @return FtpConManager
     */
    public synchronized static FtpConManager getInstance(){
    	//System.out.println("@@@@@@@@@@@11111");
        if( instance == null ){
            instance = new FtpConManager();
        }
        return instance;
    }


    /**
     * <p>ftp登录</p>
     * @param s_url ftp服务地址
     * @param uname 用户名
     * @param pass  密码
     */
    public void login(String s_url,String uname,String pass){
    	//System.out.println("@@@@@@@@@@@0000");
        ftpClient = new FTPClient();
        try{
            //连接
            ftpClient.connect(s_url);
            ftpClient.login(uname,pass);
            //检测连接是否成功
            int reply = ftpClient.getReplyCode();
            if(!FTPReply.isPositiveCompletion(reply)) {
                this.closeCon();
                System.err.println("FTP server refused connection.");
                System.exit(1);
            }
        }catch(Exception ex){
            ex.printStackTrace();
            //关闭
            this.closeCon();
        }
    }


    /**
     * <p>ftp上传文件</p>
     * @param srcUrl 须上传文件
     * @param targetFname 生成目标文件
     * @return true||false
     */
    public boolean uploadFile(InputStream inputStream,String targetFname){
    	//System.out.println("@@@@@@@@@@@22222");
        boolean flag = false;
        if( ftpClient!=null ){
        	//System.out.println("@@@@@@@@@@@22222"+ftpClient);
           // File srcFile = new File(srcUrl);
            //FileInputStream fis = null;
            try {
               // fis  = new FileInputStream(srcFile);

                //设置上传目录
                ftpClient.changeWorkingDirectory(ftppath);
                ftpClient.setBufferSize(1024);
                ftpClient.setControlEncoding("GBK");

                //设置文件类型（二进制）
                ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
                //上传
                flag = ftpClient.storeFile(targetFname, inputStream);
                //System.out.println("@@@@@@@@@@@22222"+flag);
            } catch (Exception e) {
                e.printStackTrace();
                this.closeCon();
            }finally{
                IOUtils.closeQuietly(inputStream);
            }
        }
        return flag;
    }//end method uploadFile


    /**
     *
     * <p>删除ftp上的文件</p>
     * @param srcFname
     * @return true || false
     */
    public boolean removeFile(String srcFname){
        boolean flag = false;
        if( ftpClient!=null ){
            try {
                flag = ftpClient.deleteFile(srcFname);
            } catch (IOException e) {
                e.printStackTrace();
                this.closeCon();
            }
        }
        return flag;
    }

    /**
     *<p>销毁ftp连接</p>
     */
    public void closeCon(){
        if(ftpClient !=null){
            if(ftpClient.isConnected()){
                try {
                    ftpClient.logout();
                    ftpClient.disconnect();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
